$(function () {
    document.body.addEventListener('touchstart', function () {});  //:active 测试
    $('#guanzhu-code-close').click(function () {
        if (localStorage.user) {
            var user = JSON.parse(localStorage.user);
            $.ajax({
                method: "POST",
                url: "/topfun/user/refersh",
                data: {
                    userId: user.userId,
                    token: user.token
                },
                success: function (result) {
                    var data = JSON.parse(result);
                    if (data.code == 0) {
                        user = JSON.parse(data.data);
                        if (user.favorited) {//是否关注公众号1是0否
                            if (!user.rookieAward) {//是否领取新人奖励
                                $('#gzjiangalert .dialog-cover,#gzjiangalert .alertC').fadeIn();

                                $.ajax({
                                    method: "POST",
                                    url: "/topfun/user/rookieAward",
                                    data: {
                                        userId: user.userId,
                                        token: user.token
                                    },
                                    success: function (result) {
                                        var data = JSON.parse(result);
                                        if (data.code == 0) {
                                            localStorage.user = data.data;
                                            user = JSON.parse(localStorage.user);
                                            $('#amount').html(user.amount);
                                        }
                                    }
                                });
                            }
                        }
                    } else {
                        getCode();
                    }
                }
            });
        }
    });
    $('#gzjiangalert .jiang').click(function () {
        $('#gzjiangalert .dialog-cover,#gzjiangalert .alertC').fadeOut();
    })
    $('.counter .item').click(function () {
        $(this).addClass('active').siblings().removeClass('active');
    })
    /**
     * 投注
     */
    $('#toFlip').click(function (e) {
        e.preventDefault()
        if (localStorage.user) {
            var user = JSON.parse(localStorage.user)
            var amount = $('.counter .item.active').attr('data')//选中的筹码
            if (user.amount - amount >= 0) {
                $.ajax({
                    method: "POST",
                    url: "/topfun/bet/draw",
                    data: {userId: user.userId, token: user.token, amount: amount},
                    success: function (result) {
                        var json = JSON.parse(result);
                        if (json.code === 0) {
                            console.log(json)
                            var data = JSON.parse(json.data) || {};
                            user.amount = parseInt(user.amount) - parseInt(amount); //更新缓存，没必要去后台获取
                            localStorage.user = JSON.stringify(user);
                            $('#amount').html(user.amount);
                            // location.href = "flip.html?sort=" + data.orderId;
                            $('.home').hide()
                            $('.FlipCard').show()
                            flip1(5,data)
                        } else {//code 不等于0
                            console.log(json.msg)
                        }
                    }
                })
            } else {
                $('#noMoney .dialog-cover,#noMoney .alertC,#noMoney .duisuc').fadeIn();
                $('#noMoney .sucbtn').click(function () {// 点击余额不足的充值按钮
                    $('#noMoney .dialog-cover,#noMoney .alertC,#noMoney .duisuc').fadeOut();
                    $('.dou').trigger('click')
                })
            }
        } else {
            console.log('需要登录')
        }
    })
    $('.jiang').click(function(){//再玩一局
        // window.location.href = '/index.html';
        var user = JSON.parse(localStorage.user)
        var amount = $('.counter .item.active').attr('data')//选中的筹码
        $('.cardWapper .cardItem').removeClass('active').removeClass('disabled')
        $('.cardWapper .cardItem .front').addClass('in')
        $('.cardWapper .cardItem .end').removeClass().addClass('end flip out')
        $('#nojiang .dialog-cover,#nojiang .alertC').fadeOut()
        $('#jiang .dialog-cover,#jiang .alertC').fadeOut()
        if (user.amount - amount >= 0) {
            $.ajax({
                method: "POST",
                url: "/topfun/bet/draw",
                data: {userId: user.userId, token: user.token, amount: amount},
                success: function (result) {
                    var json = JSON.parse(result);
                    if (json.code === 0) {
                        console.log(json)
                        var data = JSON.parse(json.data) || {};
                        user.amount = parseInt(user.amount) - parseInt(amount); //更新缓存，没必要去后台获取
                        localStorage.user = JSON.stringify(user);
                        $('#amount').html(user.amount);
                        flip1(5,data)
                    } else {//code 不等于0
                        console.log(json.msg)
                    }
                }
            })
        }
    })
})


var flip1 = function(count,betOrder) {
    $('.cardWapper .cardItem').off('click');
    var winningLevel = betOrder.winningLevel;
    var sort = betOrder.sort||[];
    var orderId = betOrder.orderId;
    var bonus = betOrder.bonus+'';
    var tep = [];
    var winningCount = 5;//第几张中奖 会同count --
    var winningTotal = 5;//固定
    var index = 0;
    var flag = winningLevel == 0 ? false : true;
    if(flag){
        //如果已经中奖，判断前面几张就中奖了
        var count7 = 0;
        var count8 = 0;
        var count9 = 0;
        for(var i= 0;i<5;i++){
            if(sort[i] == 7){
                count7++;
            }else if(sort[i] == 8){
                count8++;
            }else{
                count9++;
            }
            if(count7 == 3 || count8 ==3 || count9 == 3){
                winningCount = i+1;
                winningTotal = i+1;
                break;
            }
        }
    }
    var link='';
    $('a').click(function(e){
        if(count>0) {
            e.preventDefault();
            $('#duanComfirm .dialog-cover,#duanComfirm .alertC,#duanComfirm .scomfirm').fadeIn()
            link = $(this).attr('href');
        }
    })
    $('#calChange').click(function(){
        $('#duanComfirm .dialog-cover,#duanComfirm .alertC,#duanComfirm .scomfirm').fadeOut()
    })
    $('#sureChange').click(function(){
        $('#duanComfirm .dialog-cover,#duanComfirm .alertC,#duanComfirm .scomfirm').fadeOut()
        location.href= link;
    })
    $('.cardWapper .cardItem').click(function(event) {
        if(count>0) {

            if($(this).hasClass('active')) return;
            $(this).addClass('active')
            $(this).find('.end').addClass('card'+sort[5-count])
            $(this).find('.front').removeClass('in')
            $(this).find('.end').removeClass('out').addClass('in')
            count --;
            winningCount --;
            $('#totals').html(count+'/5 翻牌机会');
            if(count == 0 || winningCount == 0){
                window.setTimeout(function(){
                    tep=sort.slice((winningTotal-1),sort.length)
                    $('.cardWapper .cardItem').not('.active').each(function(){
                        index++;
                        $(this).addClass('disabled')
                        $(this).find('.end').addClass('card'+tep[index])
                    })
                    $('.cardWapper .cardItem').addClass('active')
                    $('.cardWapper .cardItem .front').removeClass('in')
                    $('.cardWapper .cardItem .end').removeClass('out').addClass('in')
                    window.setTimeout(function(){
                        if(!flag) {
                            $('#nojiang .dialog-cover,#nojiang .alertC').fadeIn()
                        }else{
                            //中奖更新用户乐豆
                            if (localStorage.user) {
                                var user = JSON.parse(localStorage.user);
                                $.ajax({
                                    method: "POST",
                                    url: "/topfun/grant/prize",
                                    data: {
                                        userId: user.userId,
                                        token: user.token,
                                        orderId:orderId,
                                    },
                                    success: function (result) {
                                        var data = JSON.parse(result);
                                        if(data.code == 0){
                                            localStorage.user = data.data;
                                            var user = JSON.parse(localStorage.user);
                                            $('#amount').html(user.amount);
                                        }
                                    }
                                });
                            }

                            $('div[name=win]').removeClass('item7 item8 item9');
                            if(winningLevel == 1){
                                $('div[name=win]').addClass('item7')
                            }else if(winningLevel == 2){
                                $('div[name=win]').addClass('item8')
                            }else{
                                $('div[name=win]').addClass('item9')
                            }
                            var bonusHtml = '';
                            for(var i=0;i<bonus.length;i++){
                                bonusHtml += '<span class="num'+bonus.substring(i,i+1)+'"></span>';
                            }
                            $('#winAmount').html(bonusHtml);
                            $('#jiang .dialog-cover,#jiang .alertC').fadeIn()
                        }
                    },500)
                },1000)
            }
        }
    });
}